<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
    <el-form label-position="right" :model="enterpriseForm" :label-width="formLabelWidth" ref="refEnterpriseForm" :rules="rules">
      <el-row>
        <el-col :span="11">
          <el-form-item label="企业名称" prop="name">
            <el-input v-model="enterpriseForm.name" placeholder="请输入企业名称" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="成立日期" prop="establishmentDate">
            <el-date-picker
                v-model="enterpriseForm.establishmentDate" type="date" placeholder="成立日期" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="注册地址" prop="registeredAddress">
            <el-input v-model="enterpriseForm.registeredAddress" placeholder="请输入注册地址" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="enterpriseForm.contactPhone" placeholder="请输入联系电话" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="enterpriseForm.email" placeholder="请输入邮箱" maxlength="64"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="官网网址" prop="officialWebsite">
            <el-input v-model="enterpriseForm.officialWebsite" placeholder="请输入官网网址" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="纳税识别号" prop="taxIdentificationNumber">
            <el-input v-model="enterpriseForm.taxIdentificationNumber" placeholder="请输入纳税识别号" maxlength="255"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import enterpriseApi from '@/api/enterpriseApi'
export default {
  name: 'EnterpriseAdd',
  components: {},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      enterpriseForm: {
        name: '',
        establishmentDate: '',
        registeredAddress: '',
        contactPhone: '',
        email: '',
        officialWebsite: '',
        taxIdentificationNumber: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        registeredAddress: [
          { required: true, message: '请输入注册地址', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        taxIdentificationNumber: [
          { required: true, message: '请输入纳税识别号', trigger: 'blur' }
        ],
      },
    }
  },
  created () {
    if (this.id) {
      enterpriseApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.enterpriseForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.enterpriseForm = {
        name: '',
        establishmentDate: '',
        registeredAddress: '',
        contactPhone: '',
        email: '',
        officialWebsite: '',
        taxIdentificationNumber: ''
      }
      this.$emit('reset-enterprise-add-event')
    },
    onSave () {
      this.$refs.refEnterpriseForm.validate((valid) => {
        if (valid) {
          enterpriseApi.save(this.enterpriseForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-enterprise-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    onBack () {
      this.$emit('close-enterprise-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>